<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="static/easyui/themes/icon.css">
<link rel="stylesheet" href="static/css/style.css" />
<link rel="stylesheet" href="static/css/tcommon.css" />
<script src="static/jquery/jquery-1.12.4.js"></script>
<script src="static/easyui/jquery.easyui.min.js"></script>
<script src="static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>物资管理</title>
</head>

<body>
	<div class="content-container" data-options="region:'center'">
		<div class="panel-header">
			<div class="panel-title">物资管理列表</div>
		</div>
		<table id="pro_table" class="easyui-datagrid" style="height: 100%">
		</table>

		<div id="pro_tb">
			<div class="search-input">
				项目：
				<input style="width: 200px;" id="projectitemid" name="projectitemid" class="easyui-combobox"
					editable="false"
					data-options="
							    valueField: 'id',
							    textField: 'name',
							    url: 'projectitemlist.do?status=2',
							    onLoadSuccess : function(data){ 
								   	if(data.length>0){
								   		$('#projectitemid').combobox('setValue',data[0].id);
							    	}
							    },
							    onSelect: function(rec){
						    		projectitemid = rec.id;
							    	if(rec.projectitemchilds.length>0){
										var url = 'projectitemchildlist?status=2&&projectitemid=' + rec.id;
										$('#projectitemchildid').combobox('reload', url);
										document.getElementById('div1').style.display = '';
							    	}else{
							    		projectitemchildid = 0;
										$('#projectitemchildid').combobox('clear');
										document.getElementById('div1').style.display = 'none';
							    		loadData();
							    	}
							    },panelHeight: 'auto'">
			</div>
			<div id="div1" style="display: none;" class="search-input">
				子项目：
				<input style="width: 200px;" id="projectitemchildid" name="projectitemchildid" class="easyui-combobox"
					editable="false"
					data-options="
							    valueField: 'id',
							    textField: 'name',
							    loadFilter: function (data) {
							        var obj = {};
							        obj.id = '';
							        obj.name = '全部';
							        data.splice(0, 0, obj);
							        return data;
							    },
							    onSelect: function(rec){
							    	projectitemchildid = rec.id;
							    	loadData();
							    },panelHeight: 'auto'">
			</div>
			<div class="search-input">
				状态：
				<select id="status" class="easyui-combobox" name="status" style="width: 120px;" editable="false"
					data-options="
							    onSelect: function(rec){
							   		status=rec.value;
							   		if(isnew){
							   			isnew = false;
							   		}else{
							   			loadData();
							   		}
							   		
							    },panelHeight: 'auto'">
					<option value="">全部</option>
					<option value="3">已拒绝</option>
					<option value="4">已审</option>
					<option value="6">待二级审核</option>
				</select>
			</div>
			<div class="search-input">
				名称：
				<input id="name" class="easyui-textbox" />
			</div>
			<div class="search-input">
				<a href="#" class="easyui-linkbutton" iconCls="icon-search" id="search">搜索</a>
			</div>
			<div>
				<a href="javascript:void(0)" class="d-btn btn-blue" onclick="newpro()">添加</a>
				<a href="javascript:void(0)" class="d-btn btn-green" onclick="updatepro()">编辑</a>
				<a href="javascript:void(0)" class="d-btn btn-red" onclick="deletepro()">删除</a>
				<!-- <a href="javascript:void(0)" class="d-btn btn-green" onclick="updatechild()">物资流动</a> -->
			</div>

		</div>
	</div>
	<div id="proDlg" class="easyui-dialog info_dialog" buttons="#pro-dlg-buttons"
		style="width: 800px; padding: 10px 20px; overflow-y: auto; top: 30px;" closed="true">
		<div class="title"></div>
		<form id="proFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 100px;">项目</td>
						<td style="width: 250px;">
							<input style="width: 200px;" id="projectitem" name="projectitem" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							   	required:true,
							    url: 'projectitemlist.do?status=2',
							    onSelect: function(rec){
								    if(rec.projectitemchilds.length>0){
										document.getElementById('tr1').style.display = '';
										document.getElementById('tr2').style.display = '';
										var url = 'projectitemchildlist?status=2&&projectitemid=' + rec.id;
										$('#projectitemchild').combobox('reload', url);
								    }else{
										document.getElementById('tr1').style.display = 'none';
										document.getElementById('tr2').style.display = 'none';
							    	}
							    },panelHeight: 'auto'">
						</td>
						<td id="tr1" style="display: none;">子项目</td>
						<td id="tr2" style="display: none;">
							<input style="width: 200px;" id="projectitemchild" name="projectitemchild" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							    onSelect: function(rec){
							    },panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">名称：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true" id="name1" name="name1" style="width: 200px;" />
						</td>
						<td style="width: 100px;">规格：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true" id="specs" name="specs"
								style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">单位：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true" id="unit" name="unit" style="width: 200px;" />
						</td>
						<td style="width: 100px;">初数：</td>
						<td>
							<input class="easyui-textbox" data-options="required:true" id="initial" name="initial"
								style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">备注：</td>
						<td colspan="4">
							<input class="easyui-textbox" id="remark" name="remark" data-options="multiline:true"
								style="width: 550px; height: 80px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">实盘：</td>
						<td>
							<input class="easyui-textbox" id="actual" name="actual" style="width: 200px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">上传文件：</td>
						<td>
							<input class="easyui-filebox" data-options="prompt:'添加文件', buttonText:'选择附件',multiple:true" id="images"
								name="images" style="width: 200px;" />
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="pro-dlg-buttons">
		<a href="# " class="easyui-linkbutton ok" onclick="savePro()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#proDlg').dialog('close')">关闭</a>
	</div>
	<div id="childDlg" class="easyui-dialog"
		style="width: 500px; padding: 10px 20px; overflow-y: auto; top: 30px;" closed="true"
		buttons="#child-dlg-buttons">
		<form id="childFm" method="post">
			<div class="content_container">
				<table class="content_table">
					<tr>
						<td style="width: 100px;">类别：</td>
						<td>
							<input type="checkbox" name="type" value="1" />
							<span style="display: inline-block; vertical-align: top; margin-top: 1px; margin-right: 10px;">增加</span>
							<input type="checkbox" name="type" value="2" />
							<span style="display: inline-block; vertical-align: top; margin-top: 1px; margin-right: 10px;">减少</span>
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">数量：</td>
						<td colspan="2">
							<input class="easyui-textbox" data-options="required:true" id="difference" name="difference"
								style="width: 200px" />
						</td>
					</tr>
				</table>
			</div>
		</form>
	</div>
	<div id="child-dlg-buttons">
		<a href="# " class="easyui-linkbutton " onclick="saveChild()">保存</a>
		<a href="# " class="easyui-linkbutton btn-cancel " onclick="javascript:$('#childDlg').dialog('close')">取消</a>
	</div>
	<div style="display: none">
		<div id="displayAtt" class="datagrid-toolbar"
			style="width: 80%; max-height: 80%; padding: 5px; top: 30px; text-align: center;"></div>
	</div>

</body>
<script>
	var currentPro;
	var currentChild;
	var status;
	var isnew = true;
	var projectitemid;
	var projectitemchildid;
	var name = "";
	function loadData() {
		if (projectitemchildid > 0){
			$('#pro_table').datagrid({
				rownumbers : true,
				singleSelect : true,
				pagination : true,
				fitColumns : false,
				border : false,
				striped : true,
				toolbar : '#pro_tb',
				url : "goodss.do",
				method : 'get',
				queryParams : {
					projectitemchildid : projectitemchildid,
					status : status,
					name : name
				},
				onSelect : function(index,row) {
					currentPro = row;
				},
				onDblClickRow : function(index,row) {
					currentPro = row;
					updatepro();
				},
				columns : [ [ {
					field : 'title',
					title : '项目名',
					width : '',
					formatter : function(value,row,index) {
						if (row.projectitemchild){
							return row.projectitemchild.name;
						}else{
							return row.item.name;
						}
					}
				},{
					field : 'name',
					title : '名称',
					width : ''
				},{
					field : 'specs',
					title : '规格',
					width : ''
				},{
					field : 'unit',
					title : '单位',
					width : ''
				},{
					field : 'initial',
					title : '初数',
					width : ''
				},{
					field : 'remark',
					title : '备注',
					width : ''
				},{
					field : 'status',
					title : '状态',
					width : '',
					formatter : function(value,row,index) {
						if (value == 4){
							return value = '已审';
						}else if (value == 3){
							return value = '已拒绝';
						}else if (value == 6){
							return value = '待二级审核';
						}
					}
				},{
					field : 'ends',
					title : '末数',
					width : ''
				},{
					field : 'actual',
					title : '实盘',
					width : ''
				},{
					field : 'reason',
					title : '拒绝理由',
					width : ''
				},{
					field : 'attachment',
					title : '附件',
					width : '',
					formatter : function(value,row,index) {
						if (row.attachments.length > 0){
							return '<a href="#" style="color:red;" onclick="displayAtt(' + index + ');">点击查看</a>';
						}else{
							return '';
						}
					}
				},{
					field : 'handle',
					title : '操作',
					width : '',
					formatter : function(value,row,index) {
						if(row.status==4){
							return '<a style="color:blue" href="#" onclick="addTab(' + index + ')">物资明细</a>'	
						}
					}
				} ] ]
			});
		}else{
			$('#pro_table').datagrid({
				rownumbers : true,
				singleSelect : true,
				pagination : true,
				fitColumns : false,
				border : false,
				striped : true,
				toolbar : '#pro_tb',
				url : "goodss.do",
				method : 'get',
				queryParams : {
					projectitemid : projectitemid,
					status : status,
					name : name
				},
				onSelect : function(index,row) {
					currentPro = row;
				},
				onDblClickRow : function(index,row) {
					currentPro = row;
					updatepro();
				},
				columns : [ [ {
					field : 'title',
					title : '项目名',
					width : '',
					formatter : function(value,row,index) {
						if (row.projectitemchild){
							return row.projectitemchild.name;
						}else{
							return row.item.name;
						}
					}
				},{
					field : 'name',
					title : '名称',
					width : ''
				},{
					field : 'specs',
					title : '规格',
					width : ''
				},{
					field : 'unit',
					title : '单位',
					width : ''
				},{
					field : 'initial',
					title : '初数',
					width : ''
				},{
					field : 'remark',
					title : '备注',
					width : ''
				},{
					field : 'status',
					title : '状态',
					width : '',
					formatter : function(value,row,index) {
						if (value == 4){
							return value = '已审';
						}else if (value == 3){
							return value = '已拒绝';
						}else if (value == 6){
							return value = '待二级审核';
						}
					}
				},{
					field : 'ends',
					title : '末数',
					width : ''
				},{
					field : 'actual',
					title : '实盘',
					width : ''
				},{
					field : 'reason',
					title : '拒绝理由',
					width : ''
				},{
					field : 'attachment',
					title : '附件',
					width : '',
					formatter : function(value,row,index) {
						if (row.attachments.length > 0){
							return '<a href="#" style="color:red;" onclick="displayAtt(' + index + ');">点击查看</a>';
						}else{
							return '';
						}
					}
				},{
					field : 'handle',
					title : '操作',
					width : '',
					formatter : function(value,row,index) {
						if(row.status==4){
							return '<a style="color:blue" href="#" onclick="addTab(' + index + ')">物资明细</a>'	
						}
					}
				} ] ]
			});
		}
		if (status != 3){
			$('#pro_table').datagrid('hideColumn', 'reason');
		}
	}
	function refreshProTable() {
		$('#pro_table').datagrid("reload");
	}
	
	$("#search").click(function() {
		name = $("#name").textbox('getValue');
		loadData();
	});
	function newpro() {
		$("#proDlg").dialog('open').dialog('setTitle', '添加物资');
		$("#proFm").form('clear');
		currentPro = {};
		$("#projectitem").combobox("enable");
		document.getElementById('tr1').style.display = 'none';
		document.getElementById('tr2').style.display = 'none';
	}
	function updatepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if (currentPro){
			$("#proDlg").dialog('open').dialog('setTitle', '更新预算明细');
			$("#projectitem").combobox("setValue", currentPro.item);
			$("#projectitem").combobox("disable");
			$("#projectitemchild").combobox("setValue", currentPro.projectitemchild);
			$("#projectitemchild").combobox("disable");
			$("#proFm").form('load', {
				name1 : currentPro.name,
				specs : currentPro.specs,
				unit : currentPro.unit,
				initial : currentPro.initial,
				remark : currentPro.remark,
				actual : currentPro.actual
			});
			$("#proDlg").dialog('open').dialog('setTitle', '预算明细详细');
		}else{
			$.messager.alert('更新', '请选择要更新的物资', 'warning');
		}
	}
	function deletepro() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if (currentPro){
			if (currentPro.status == 6){
				$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
					if (data){
						$.ajax({
							type : 'DELETE',
							url : "goodsDelete.do?id=" + currentPro.id,
							contentType : "application/json",
							success : function(data) {
								if (data.success){
									$.messager.show({
										title : '删除',
										msg : '删除成功',
										timeout : 5000,
										showType : 'slide'
									});
									loadData();
								}else{
									$.messager.alert('删除', '删除失败', 'error');
								}
							},
							dataType : "json"
						});
					}
				});
			}else{
				$.messager.alert('警告', '已审核物资不允许删除', 'warning');
			}
		}else{
			$.messager.alert('更新', '请选择要删除的物资', 'warning');
		}
	}
	function savePro() {
		if (!$("#proFm").form('validate'))
			return;
		var formData = new FormData();
		if (currentPro.id){
			formData.append("id", currentPro.id);
			formData.append("status", currentPro.status);
		}else{
			formData.append("status", 6);
		}
		formData.append("projectitemid", $("#projectitem").combobox('getValue'));
		formData.append("projectitemchildid", $("#projectitemchild").combobox('getValue'));
		formData.append("name", $("#name1").textbox('getValue'));
		formData.append("specs", $("#specs").textbox('getValue'));
		formData.append("unit", $("#unit").textbox('getValue'));
		formData.append("initial", $("#initial").textbox('getValue'));
		formData.append("actual", $("#actual").textbox('getValue'));
		formData.append("remark", $("#remark").textbox('getValue'));
		var images = document.getElementById("filebox_file_id_1").files;
		for(var i = 0;i < images.length;i++){
			formData.append('images', images[i]);
		}
		$.ajax({
			type : 'POST',
			url : "goodsSave.do",
			cache : false,
			data : formData,
			processData : false,
			contentType : false,
			success : function(data) {
				if (data.success){
					$.messager.show({
						title : '保存',
						msg : '保存成功',
						timeout : 5000,
						showType : 'slide'
					});
					loadData();
				}else{
					$.messager.alert('保存', '保存失败', 'error');
				}
				$('#proDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	$("input[name='type']:checkbox").click(function() {
		var flag = $(this).is(':checked');
		if (flag){
			$(this).siblings("input").attr("checked", false);
		}
	});
	function updatechild() {
		currentPro = $("#pro_table").datagrid('getSelected');
		if (currentPro.status == 4){
			$("#childDlg").dialog('open').dialog('setTitle', '添加物资明细');
			$("#childFm").form('clear');
			currentChild = {};
		}else if (currentPro.status == 3){
			$.messager.alert('更新', '审批未通过', 'warning');
		}else if (currentPro.status == 6){
			$.messager.alert('更新', '未审批物资禁止流动', 'warning');
		}else{
			$.messager.alert('更新', '请选择要更新的物资', 'warning');
		}
	}
	function saveChild() {
		if (!$("#childFm").form('validate'))
			return;
		var type = $('input:checkbox[name="type"]:checked').val();
		currentChild.type = type;
		currentChild.difference = $("#difference").textbox('getValue');
		currentChild.goodsid = currentPro.id;
		$.ajax({
			type : 'POST',
			url : "goodsCurrentSave.do",
			data : JSON.stringify(currentChild),
			contentType : "application/json",
			success : function(data) {
				if (data.success){
					$.messager.show({
						title : '保存物资明细',
						msg : '保存物资明细成功',
						timeout : 5000,
						showType : 'slide'
					});
					refreshProTable();
				}else{
					$.messager.alert('保存物资明细', '保存物资明细失败', 'error');
				}
				$('#childDlg').dialog('close');
			},
			dataType : "json"
		});
	}
	function displayAtt(index) {
		var row = $('#pro_table').datagrid('getRows')[index];
		var tb = "";
		if(row.status == 6){
			$.each(row.attachments,function(i,val) {
				if(isImage(val.url)){
					tb+= '<img onclick="window.open(\'../'+val.url+ 
					'\',\'_blank\', \' toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no\');" '+
					'src="../'+val.url+'" style="width:100px;"><br><span>'+val.name+'</span><br>';
					tb+= '<a href="javascript:void(0)" onclick="remove('+val.id+')"><span>删除</span></a><br>'
				}else{
					tb+= '<a href="javascript:void(0)" onclick="attachmentFile('+val.id+',\''+val.name+
					'\')"><span>'+val.name+'</span></a><br>';
					tb+= '<a href="javascript:void(0)" onclick="remove('+val.id+')"><span>删除</span></a><br>'
				}
			});
		}else{
			$.each(row.attachments,function(i,val) {
				if(isImage(val.url)){
					tb+= '<img onclick="window.open(\'../'+val.url+ 
					'\',\'_blank\', \' toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no\');" '+
					'src="../'+val.url+'" style="width:100px;"><br><span>'+val.name+'</span><br>';
				}else{
					tb+= '<a href="javascript:void(0)" onclick="attachmentFile('+val.id+',\''+val.name+
					'\')"><span>'+val.name+'</span></a><br>';
				}
			});
		}
		$('#displayAtt').empty().html(tb);
		$('#displayAtt').dialog({
			title : '附件',
			resizable : true,
			closed : false,
			cache : false,
			modal : true
		});
	}
	function isImage(url) {
		var index= url.lastIndexOf(".");
		var ext = url.substr(index+1);
		return [ 'png', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff' ,'ico'].
				indexOf(ext.toLowerCase()) !== -1;
	}
	function attachmentFile(id,name) {
		$.get('attachmentFile.do?id=' + id, function(data) {
			blob = new Blob([ data ])
			var a = document.createElement('a');
			a.download = name;
			a.href = window.URL.createObjectURL(blob)
			a.click()
		})
	}
	function remove(id) {
		$.messager.confirm("操作提示", "您确定要执行操作吗？", function(data) {
			if (data){
				$.ajax({
					type : 'POST',
					url : "attachmentDelete.do?id=" + id,
					contentType : "application/json",
					success : function(data) {
						if (data.success){
							$("#att" + id).remove();
							$.messager.show({
								title : '删除附件',
								msg : '删除附件成功',
								timeout : 5000,
								showType : 'slide'
							});
							refreshProTable();
						}else{
							$.messager.alert('删除附件', '删除附件失败', 'error');
						}
						$('#childDlg').dialog('close');
					},
					dataType : "json"
				});
			}
		});
	}
	function addTab(index) {
		var item = $("#pro_table").datagrid('getRows')[index];
		var title = '物资管理-' + item.item.name;
		window.parent.window.addTab(title, "goodscurrent.do?goodsid=" + item.id, item.id);
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear(),month = date.getMonth() + 1, //月份是从0开始的
		day = date.getDate(),hour = date.getHours(),min = date.getMinutes(),sec = date.getSeconds();
		var newTime = year + '-' + month + '-' + day;
		return newTime;
	}
	$.extend($.fn.textbox.defaults.rules, {
		number : {//value值为文本框中的值
			validator : function(value) {
				var reg = /^\d+(\.\d+)?$/;
				return reg.test(value);
			},
			message : "请输入0-100之间的数字"
		},
		chinese : {
			validator : function(value,param) {
				var reg = /^[\u4e00-\u9fa5]+$/i;
				return reg.test(value);
			},
			message : "请输入中文"
		},
		checkLength : {
			validator : function(value,param) {
				return param[0] >= get_length(value);
			},
			message : '请输入最大{0}位字符'
		},
		specialCharacter : {
			validator : function(value,param) {
				var reg = new RegExp("[`~!@#$^&*()=|{}':;'\\[\\]<>~！@#￥……&*（）——|{}【】‘；：”“'、？]");
				return !reg.test(value);
			},
			message : '不允许输入特殊字符'
		},
		end : {
			validator : function(value,param) {
				var startDate = $("#startdate").val();
				var startTmp = new Date(startDate.replace(/-/g, "/"));
				var endTmp = new Date(value.replace(/-/g, "/"));
				return startTmp <= endTmp;
			},
			message : '结束时间不能早于开始时间！'
		},
		remainmoney : {
			validator : function(value,param) {
				return parseFloat(value) <= parseFloat(remainmoney);
			},
			message : '不能超过剩余金额'
		}
	});
</script>
</html>